local M = {}

M.baseGroup = nil

local ran = math.rad(90/2)
local screenScale = (display.contentHeight)/math.tan(ran)
local cellNum = 4
local sin = math.sin
local cos = math.cos
local degToRad = math.pi/180
local radToDeg = 180/math.pi
local sqrt = math.sqrt
local acos = math.acos
--[[每个面的配置
 	面被抽象为总长度为4的平面
 	origPos 为每个面的起始位置
 	offset1:行切割轴向 offset2:列切割轴向
 ]]
local tCubeCfg = {
	up = {
		_name = "up",
		imgPath = "assets/unpack/sky_box/sky-up.png",
		origPos = {-1, 1, -1}, offset = {3, 1}, offset1 = {0, 0, 0.5}, offset2 = {0.5, 0, 0},
	},
	left = {
		_name = "left",
		imgPath = "assets/unpack/sky_box/sky-left.png",
		origPos = {-1, 1, -1}, offset = {-2, 3}, offset1 = {0, -0.5, 0}, offset2 = {0, 0, 0.5},
	},
	back = {
		_name = "back",
		imgPath = "assets/unpack/sky_box/sky-back.png",
		origPos = {1, 1, -1}, offset = {-2, -1}, offset1 = {0, -0.5, 0}, offset2 = {-0.5, 0, 0},
	},
	down = {
		_name = "down",
		imgPath = "assets/unpack/sky_box/sky-down.png",
		origPos = {-1, -1, 1}, offset = {-3, 1}, offset1 = {0, 0, -0.5}, offset2 = {0.5, 0, 0},
	},
	right = {
		_name = "right",
		imgPath = "assets/unpack/sky_box/sky-right.png",
		origPos = {1, 1, 1}, offset = {-2, -3}, offset1 = {0, -0.5, 0}, offset2 = {0, 0, -0.5},
	},
	front = {
		_name = "front",
		imgPath = "assets/unpack/sky_box/sky-front.png",
		origPos = {-1, 1, 1}, offset = {-2, 1}, offset1 = {0, -0.5, 0}, offset2 = {0.5, 0, 0},
	},	
}

local function processSingleSurface(params)
	local imgPath = params.imgPath
	local origPos = params.origPos
	local offset = params.offset

	local imgSheet = graphics.newImageSheet(imgPath, {
			width = 128, height = 128,
			numFrames = cellNum * cellNum
		}
	)
	local g1 = display.newGroup()
	g1:scale(0.25, 0.25)

	g1.x, g1.y = SCREEN_CX - 256,  SCREEN_CY - 256
	-- 行切割方向
	local offsetLine = offset[1]
	local abs_offsetLine = math.abs(offsetLine)
	local dir_line = offsetLine/abs_offsetLine/2

	-- 列切割方向
	local offsetRow = offset[2]
	local abs_offsetRow = math.abs(offsetRow)
	local dir_row = offsetRow/abs_offsetRow/2

	-- 不变的一个维度
	local function getUnchangeDir()
		if (abs_offsetLine == 1 and abs_offsetRow == 2) or (abs_offsetLine == 2 and abs_offsetRow == 1) then
			return 3
		end
		if (abs_offsetLine == 1 and abs_offsetRow == 3) or (abs_offsetLine == 3 and abs_offsetRow == 1) then
			return 2
		end
		if (abs_offsetLine == 2 and abs_offsetRow == 3) or (abs_offsetLine == 3 and abs_offsetRow == 2) then
			return 1
		end
	end

	local unchangeDir = getUnchangeDir()

	-- 确认每个点的位置
	local tCell = {}
	for line = 1, cellNum do
		local index1 = (line - 1)*cellNum

		local begin1 = origPos[abs_offsetLine] + (line - 1)*dir_line

		for row = 1, cellNum do
			-- if params._name == "front" then
			-- 	loger.error(abs_offsetLine, begin1)
			-- end
			local index2 = index1 + row
			tCell[index2] = {}

			local begin2 = origPos[abs_offsetRow] + (row - 1)*dir_row
			-- if params._name == "front" then
			-- 	loger.error(abs_offsetRow, begin2)
			-- end
			-- 切割图片
			local img = display.newImage(imgSheet, index1 + row)
			tCell[index2].img = img

			img.anchorX = 0
			img.anchorY = 0
			img.x = 0
			img.y = 0
			img.isVisible = false
			g1:insert(img)
			-- loger.error(unchangeDir)
			tCell[index2].p = {}
			-- 图片的位置
			tCell[index2].p[1] = {}
			tCell[index2].p[1][unchangeDir] = origPos[unchangeDir]
			tCell[index2].p[1][abs_offsetLine] = begin1
			tCell[index2].p[1][abs_offsetRow] = begin2

			tCell[index2].p[2] = {}
			tCell[index2].p[2][unchangeDir] = origPos[unchangeDir]
			tCell[index2].p[2][abs_offsetLine] = begin1 + dir_line
			tCell[index2].p[2][abs_offsetRow] = begin2

			tCell[index2].p[3] = {}
			tCell[index2].p[3][unchangeDir] = origPos[unchangeDir]
			tCell[index2].p[3][abs_offsetLine] = begin1 + dir_line
			tCell[index2].p[3][abs_offsetRow] = begin2 + dir_row

			tCell[index2].p[4] = {}
			tCell[index2].p[4][unchangeDir] = origPos[unchangeDir]
			tCell[index2].p[4][abs_offsetLine] = begin1
			tCell[index2].p[4][abs_offsetRow] = begin2 + dir_row


			-- if params._name == "front" then
			-- 	loger.error(tCell[index2].p)
			-- end
		end
	end

	return tCell
end

-- 每一张被切割后的图
local function calcPos(params, xAngle, yAngle)
	local img = params.img

	img.isVisible = true

	local function rotation(p, xAngle, yAngle)
		local p1 = {}

		for i = 1, 4 do
			local x, y, z = p[i][1], p[i][2], p[i][3]

			local sinx, cosx = sin(xAngle*degToRad), cos(xAngle*degToRad)

			local siny, cosy = sin(yAngle*degToRad), cos(yAngle*degToRad)
			-- 绕x轴旋转
			y, z = y*cosx + z*sinx, z*cosx - y*sinx

			-- 绕y轴旋转
			x, z = x*cosy - z*siny, x*siny + z*cosy

			p1[#p1 + 1] = {x, y, z}
		end
		return p1
	end

	local function changePath(p)
		-- print(p[1][1])
		local tPoint = {}
		local isNotVisible = false
		for i = 1, 4 do
			
			-- 计算实际屏幕位置
			local p1 = p[i]
			local z = p1[3]
			local x = (p1[1] + 1)/2
			local y = (-p1[2] + 1)/2 -- 构造三维系统的时候，y向上
			
			if z > 0.01 then
				-- isNotVisible = false
			end

			tPoint[i] = {x, y, z}
		end

		-- 计算面的法向量
		local x1, y1, z1 = p[2][1] - p[1][1], p[2][2] - p[1][2], p[2][3] - p[1][3]
		local x2, y2, z2 = p[3][1] - p[2][1], p[3][2] - p[2][2], p[3][3] - p[2][3]
		
		-- 法向量
		local x3, y3, z3 = y1*z2-z1*y2, z1*x2-x1*z2, x1*y2-y1*x2

		-- if params._name == "front" then
		-- 	loger.error(p, x1, y1, z1, "bbb", x2, y2, z2, "ccc", x3, y3, z3)
		-- end


		-- 计算与视锥的夹角
		local n1, n2, n3 = 0, 0, 1

		-- 计算点乘值
		local fenmu = sqrt(x3^2 + y3^2 + z3^2)*1
		local cosNum = (x3*n1 + y3*n2 + z3*n3)/fenmu

		local deg = acos(cosNum)*radToDeg

		if deg > 90 then		
			img.isVisible = false
		else
			img.isVisible = true
			img.path.x1 = tPoint[1][1]*screenScale
			img.path.y1 = tPoint[1][2]*screenScale

			img.path.x2 = tPoint[2][1]*screenScale
			img.path.y2 = tPoint[2][2]*screenScale - 128

			img.path.x3 = tPoint[3][1]*screenScale - 128
			img.path.y3 = tPoint[3][2]*screenScale - 128

			img.path.x4 = tPoint[4][1]*screenScale - 128
			img.path.y4 = tPoint[4][2]*screenScale
		end
	end

	local p = rotation(params.p, xAngle, yAngle)
	changePath(p)
end

local function update(tCells)
	local xAngle, yAngle = 45, 0

	local function update1()
		-- xAngle = xAngle + 1
		yAngle = yAngle + 1
		for k, tCell in pairs(tCells) do
			for index, info in ipairs(tCell) do
				-- info._name = tCell._name
				calcPos(info, xAngle, yAngle)
			end
		end
	end

	-- update1()

	Runtime:addEventListener( "enterFrame", update1 )
end

function M.new(params)
	M.baseGroup = display.newGroup()

	local tCells = {}

	for k, surfaceCfg in pairs(tCubeCfg) do
		local tCell = processSingleSurface(surfaceCfg)
		tCell._name = surfaceCfg._name
		tCells[#tCells + 1] = tCell
	end
	for k, tCell in pairs(tCells) do
		for index, info in ipairs(tCell) do
			info._name = tCell._name
		end
	end

	update(tCells)
	-- loger.error(#tCells)
	

	return M.baseGroup
end


return M